package com.sskj.gocoin.p.activity;

import android.app.AlertDialog;
import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.HttpData;
import com.sskj.gocoin.m.bean.PayWayBean;
import com.sskj.gocoin.m.bean.UserInfo;
import com.sskj.gocoin.m.http.JsonCallBack;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.SecurityCenterActivity;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class SecurityCenterPresenter extends BasePresenter<SecurityCenterActivity> {
    public void request() {
        OkGo.<HttpData<UserInfo>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_INFO)
                .params("mobile", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData<UserInfo>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == 200) {
                            SPUtils.putBean(mView, Constants.SP_USER_INFO, httpData.getData());
                            SPUtils.put(mView, Constants.SP_REALNAME, httpData.getData().getRealname());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, httpData.getData().getStatus());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS_HIGH, httpData.getData().getAuth_status());
                            mView.updateStatus(httpData.getData());
                        }
                    }
                });
    }

    public void requsetPayWayList() {
        OkGo.<HttpData<List<PayWayBean>>>post(HttpConfig.BASE_URL + HttpConfig.PAY_WAY_LIST)
                .params("account", MyAppLication.getAccount())
                .execute(new JsonCallBack<HttpData<List<PayWayBean>>>(this, false) {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<PayWayBean>>> response) {
                        mView.onPayWaySuccess(response.body());
                    }

                    @Override
                    public void onError(Response<HttpData<List<PayWayBean>>> response) {
                        super.onError(response);
                        mView.onHttpFail();
                    }
                });
    }

    public void requsetCheckSwitch(PayWayBean item) {
        int status = item.getStatus() == 0 ? 1 : 0;
        OkGo.<Object>post(HttpConfig.BASE_URL + HttpConfig.CHANGE_SWITCH)
                .params("type", item.getType())
                .params("val", status)
                .params("account", MyAppLication.getAccount())
                .execute(new JsonCallBack<Object>(this) {
                    @Override
                    protected void _onSuccess(Response<Object> response) {
                        item.setStatus(status);
                        mView.onChangeSwitchSuccess(item);
                    }
                });
    }

    public void requestAddPayWay(String type, String selectName, String bankName, String bankBranch, String bankNum, String aliaccount, String imagePath, String payPwd) {

        HttpParams params = new HttpParams();
        if (!TextUtils.isEmpty(imagePath)) {
            File file = new File(imagePath);
            params.put("wx_img", file);
            params.put("ali_img ", file);
        }
        params.put("act", type);
        params.put("type", selectName);
        params.put("bank", bankName);
        params.put("branch", bankBranch);
        params.put("bank_car", bankNum);
        params.put("wx", aliaccount);
        params.put("alipay", aliaccount);
        params.put("pal_account", aliaccount);
        params.put("tpwd", payPwd);
        params.put("account", MyAppLication.getAccount());
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.ADD_PAY_PWD)
                .params(params)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<Object>> response) {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            requsetPayWayList();
                            mView.onChangePayWaySuccess();
                        }
                    }
                });
    }

    public void requestRelieveBusiness(AlertDialog alertDialog) {
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.RELIEVE_BUSINESS)
                .params("account", MyAppLication.getAccount())
                .execute(new JsonCallBack<HttpData<Object>>() {
                    @Override
                    protected void _onSuccess(Response<HttpData<Object>> response) {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(response.body().getMsg());
                        if (alertDialog.isShowing()) {
                            alertDialog.dismiss();
                        }
                        mView.onRelieveBusinessSuccess();
                    }
                });
    }

    public void toggleGoogleVerify(int googleType, String code, String mobileCode, String mobile) {
        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.SET_GOOGLE_VERIFY)
                .params("act", googleType)
                .params("dyGoodleCommand", code)
                .params("code", mobileCode)
                .params("mobile", mobile)
                .execute(new JsonCallBack<HttpData<Object>>() {
                    @Override
                    protected void _onSuccess(Response<HttpData<Object>> response) {
                       mView.setGoogleVerifySuccess();
                    }
                });

    }

    public void sendEmail() {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", MyAppLication.getMobile())
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(response.body().getMsg());
                        mView.onAuthCodeSuccess();
                    }
                });
    }

    public void requestAuthCode(String account) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                .params("mobile", account)
                .params("type", 7)
                .execute(new JsonCallBack<HttpData>(this, true) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(response.body().getMsg());
                            mView.onAuthCodeSuccess();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                    }
                });

    }
}
